package com.project.market.service.impl;

import com.github.pagehelper.PageHelper;
import com.project.market.db.domain.MarketCoupon;

import com.project.market.db.domain.MarketCouponExample;
import com.project.market.db.mapper.MarketCouponMapper;
import com.project.market.service.AdminCouponService;
import com.project.market.util.MybatisUtil;
import org.apache.ibatis.session.SqlSession;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

public class AdminCouponServiceImpl implements AdminCouponService {

    @Override
    public List<MarketCoupon> list(String page, String limit, String sort, String order,String name,String type,String status) {
        SqlSession session = MybatisUtil.getSession();
        MarketCouponMapper mapper = session.getMapper(MarketCouponMapper.class);
        MarketCouponExample example = new MarketCouponExample();
        MarketCouponExample.Criteria criteria = example.createCriteria();
        criteria.andDeletedEqualTo(false);

        try {

            if (name != null && !"".equals(name)) {
                criteria.andNameLike("%" + name + "%");
            }
            if (type != null && !"".equals(type)) {
                criteria.andTypeEqualTo(Short.parseShort(type));
            }
            if (status != null && !"".equals(status)) {
                criteria.andStatusEqualTo(Short.parseShort(status));
            }
        }
        catch (Exception e){
            return null;
        }

        example.setOrderByClause(sort + " " + order);
        List<MarketCoupon> list = mapper.selectByExample(example);


        PageHelper.startPage(Integer.valueOf(page), Integer.valueOf(limit));

        session.commit();
        session.close();
        return list;
    }

    @Override
    public MarketCoupon create(String name, String total, String discount, String min, String limit, String type, String status, String goodsType, List<Integer> goodsValue, String timeType, String days, String startTime, String endTime) {

        SqlSession session = MybatisUtil.getSession();
        MarketCouponMapper mapper = session.getMapper(MarketCouponMapper.class);
        MarketCoupon marketCoupon = new MarketCoupon();
        marketCoupon.setName(name);
        marketCoupon.setTotal(Integer.valueOf(total));
        marketCoupon.setDiscount(BigDecimal.valueOf(Integer.valueOf(discount)));
        marketCoupon.setMin(BigDecimal.valueOf(Integer.valueOf(min)));
        marketCoupon.setLimit(Short.valueOf(limit));
        marketCoupon.setType(Short.valueOf(type));
        marketCoupon.setStatus(Short.valueOf(status));
        marketCoupon.setGoodsType(Short.valueOf(goodsType));
        marketCoupon.setGoodsValue(listToInt(goodsValue));
        marketCoupon.setTimeType(Short.valueOf(timeType));
        marketCoupon.setDays(Short.valueOf(days));
        marketCoupon.setDeleted(false);

        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        if (!startTime.equals("null")) {
            marketCoupon.setStartTime(LocalDateTime.parse(startTime, df));
        }
        if(!endTime.equals("null")) {
            marketCoupon.setEndTime(LocalDateTime.parse(endTime, df));
        }
        marketCoupon.setAddTime(LocalDateTime.now());
        marketCoupon.setUpdateTime(LocalDateTime.now());

        mapper.insert(marketCoupon);

        session.commit();
        session.close();

        list("1","20","add_time","desc",null,null,null);

        return marketCoupon;
    }

    Integer[] listToInt(List<Integer> goodsValue) {

        Integer[] integers = new Integer[goodsValue.size()];

        for (int i = 0; i < goodsValue.size(); i++) {
            integers[i] = goodsValue.get(i);
        }

        return integers;
    }


}
